export default function loadModel() {
    let modelName = 'enclosure';
    let scope = this;
    let objLoader = new THREE.OBJLoader2();
    let callbackOnLoad = function (event) {
        let object3Dlayer = new AMap.Object3DLayer();
        let meshes = event.detail.loaderRootNode.children;
        for (let i = 0; i < meshes.length; i++) {
            let vecticesF3 = meshes[i].geometry.attributes.position;
            let vecticesNormal3 = meshes[i].geometry.attributes.normal;
            let vecticesUV2 = meshes[i].geometry.attributes.uv;

            let vectexCount = vecticesF3.count;

            mesh = new AMap.Object3D.MeshAcceptLights();

            let geometry = mesh.geometry;

            //底部一圈
            // debugger

            let c, opacity;

            let material = meshes[i].material[0] || meshes[i].material;
            // debugger
            if (material.map)
                mesh.textures.push('https://a.amap.com/jsapi_demos/static/demo-center/model/1519/1519.bmp')

            c = material.color;
            opacity = material.opacity

            // debugger
            for (let j = 0; j < vectexCount; j += 1) {
                let s = j * 3;
                geometry.vertices.push(vecticesF3.array[s], vecticesF3.array[s + 2], -vecticesF3.array[s + 1]);

                if (vecticesNormal3) {
                    geometry.vertexNormals.push(vecticesNormal3.array[s], vecticesNormal3.array[s + 2], -vecticesNormal3.array[s + 1]);
                }
                if (vecticesUV2) {
                    geometry.vertexUVs.push(vecticesUV2.array[j * 2], 1 - vecticesUV2.array[j * 2 + 1]);
                }
                geometry.vertexColors.push(c.r, c.g, c.b, opacity)
            }
            // debugger
            mesh.DEPTH_TEST = material.depthTest
            // mesh.backOrFront = 'both'
            mesh.transparent = opacity < 1;
            mesh.scale(6, 6, 6)
            mesh.rotateZ(-48)
            mesh.position(new AMap.LngLat(116.472605, 39.992075))
            object3Dlayer.add(mesh)
        }
        map.add(object3Dlayer)
    };

    let onLoadMtl = function (materials) {
        objLoader.setModelName(modelName);
        objLoader.setMaterials(materials);
        objLoader.load('https://a.amap.com/jsapi_demos/static/demo-center/model/1519/1519.obj', callbackOnLoad, null, null, null, false);
    };
    objLoader.loadMtl('https://a.amap.com/jsapi_demos/static/demo-center/model/1519/1519.mtl', null, onLoadMtl);
};